package bsf.baseservice.businessmq.dal;

import bsf.baseservice.businessmq.model.tb_consumer_partition_model;
import bsf.db.DbConn;
import bsf.util.ConvertUtil;

import java.util.List;
import java.util.Map;

public class tb_consumer_partition_dal {
    public tb_consumer_partition_model get(DbConn PubConn, int consumerclientid, int partitionid) {
        //�����߿ͻ���ID
        //������ID
        StringBuilder stringSql = new StringBuilder();
        stringSql.append("select s.* from tb_consumer_partition s WITH(NOLOCK) where s.consumerclientid=? and s.partitionid=?");
        List<Map<String, Object>> ds = PubConn.executeList(stringSql.toString(), new Object[]{consumerclientid, partitionid});
        if (ds != null && ds.size() > 0) {
            return createModel(ds.get(0));
        }
        return null;
    }

    public int updateLastMQID(DbConn PubConn, int consumerclientid, int partitionid, long lastmqid) {
        //�����߿ͻ���ID
        //������ID
        //
        int rev = PubConn.executeSql("update tb_consumer_partition WITH (ROWLOCK) set lastmqid=?, lastupdatetime=getdate() " +
                "where consumerclientid=? and partitionid=? and lastmqid<?", new Object[]{lastmqid, consumerclientid, partitionid, lastmqid});
        return rev;

    }

    public int edit2(DbConn PubConn, tb_consumer_partition_model model) {
        //�����߿ͻ���ID
        //
        //������ID
        //
        int rev = PubConn.executeSql("update tb_consumer_partition WITH (ROWLOCK) " +
                        "set partitionindex=?,lastconsumertempid=? " +
                        "where consumerclientid=? and partitionid=?",
                new Object[]{model.partitionindex, model.lastconsumertempid, model.consumerclientid, model.partitionid});
        return rev;


    }

    public boolean add2(DbConn PubConn, tb_consumer_partition_model model) {
        //�����߿ͻ���ID
        //
        //������ID
        //
        //������ѵ�MQID


        int rev = PubConn.executeSql("insert into tb_consumer_partition" +
                        "(consumerclientid,partitionindex,partitionid,lastconsumertempid,lastmqid,lastupdatetime,createtime)" +
                        " values(?,?,?,?,?,getdate(),getdate())",
                new Object[]{model.consumerclientid, model.partitionindex, model.partitionid, model.lastconsumertempid, model.lastmqid});
        return rev == 1;

    }


    public tb_consumer_partition_model createModel(Map<String, Object> dr) {
        tb_consumer_partition_model o = new tb_consumer_partition_model();

        //
        if (dr.containsKey("id")) {
            o.id = ConvertUtil.objToInt(dr.get("id"));
        }
        //�����߿ͻ���ID
        if (dr.containsKey("consumerclientid")) {
            o.consumerclientid = ConvertUtil.objToInt(dr.get("consumerclientid"));
        }
        //
        if (dr.containsKey("partitionindex")) {
            o.partitionindex = ConvertUtil.objToInt(dr.get("partitionindex"));
        }
        //������ID
        if (dr.containsKey("partitionid")) {
            o.partitionid = ConvertUtil.objToInt(dr.get("partitionid"));
        }
        //
        if (dr.containsKey("lastconsumertempid")) {
            o.lastconsumertempid = ConvertUtil.objToInt64(dr.get("lastconsumertempid"));
        }
        //������ѵ�MQID
        if (dr.containsKey("lastmqid")) {
            o.lastmqid = ConvertUtil.objToInt64(dr.get("lastmqid"));
        }
        //���������ִ��ʱ��(�Ե�ǰ��ʱ��Ϊ׼)
        if (dr.containsKey("lastupdatetime")) {
            o.lastupdatetime = ConvertUtil.objToDateTime(dr.get("lastupdatetime"));
        }
        //�����߷�������ʱ��(�Ե�ǰ��ʱ��Ϊ׼)
        if (dr.containsKey("createtime")) {
            o.createtime = ConvertUtil.objToDateTime(dr.get("createtime"));
        }
        return o;
    }
}